#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Python基础：函数
"""

def greet(name):
    """
    简单的问候函数
    :param name: 姓名
    :return: 问候语
    """
    return f"你好, {name}!"

def calculate_area(length, width):
    """
    计算矩形面积
    :param length: 长度
    :param width: 宽度
    :return: 面积
    """
    return length * width

def calculate_circle_area(radius, pi=3.14159):
    """
    计算圆的面积
    :param radius: 半径
    :param pi: 圆周率，默认值为3.14159
    :return: 圆的面积
    """
    return pi * radius ** 2

def sum_numbers(*args):
    """
    计算任意数量数字的和
    :param args: 可变参数
    :return: 数字总和
    """
    total = 0
    for num in args:
        total += num
    return total

def create_profile(**kwargs):
    """
    创建个人资料
    :param kwargs: 关键字参数
    :return: 个人资料字典
    """
    profile = {}
    for key, value in kwargs.items():
        profile[key] = value
    return profile

def factorial(n):
    """
    递归计算阶乘
    :param n: 数字
    :return: 阶乘结果
    """
    if n == 0 or n == 1:
        return 1
    else:
        return n * factorial(n - 1)

def demonstrate_lambda():
    """演示lambda函数"""
    # 普通函数
    def add(x, y):
        return x + y
    
    # lambda函数
    add_lambda = lambda x, y: x + y
    
    print(f"普通函数结果: {add(3, 5)}")
    print(f"Lambda函数结果: {add_lambda(3, 5)}")
    
    # 在排序中的应用
    students = [("张三", 85), ("李四", 90), ("王五", 78)]
    # 按成绩排序
    students_sorted = sorted(students, key=lambda x: x[1])
    print(f"按成绩排序: {students_sorted}")

if __name__ == "__main__":
    print("=== 函数定义和调用示例 ===")
    print(greet("张三"))
    
    area = calculate_area(5, 3)
    print(f"矩形面积 (5×3): {area}")
    
    circle_area = calculate_circle_area(4)
    print(f"圆的面积 (半径=4): {circle_area}")
    
    total = sum_numbers(1, 2, 3, 4, 5)
    print(f"数字总和 (1+2+3+4+5): {total}")
    
    profile = create_profile(name="张三", age=25, city="北京")
    print(f"个人资料: {profile}")
    
    fact = factorial(5)
    print(f"5的阶乘: {fact}")
    
    print("\n=== Lambda函数示例 ===")
    demonstrate_lambda()